﻿define([
    'Xjs/core/util',
    'Xjs/ui/panel/Panel',
    'Xjs/ui/grid/PropertyGroup' // Include the proper
],
function (util, Panel) {
    'use strict';

    var type = 'propertyGrid',
    PropertyGrid = Xjs.getType(type);

    if (PropertyGrid) { // It has been already loaded
        return PropertyGrid;
    }

    PropertyGrid = util.define({
        base: Panel,
        construct: function (conf, parent) {

            PropertyGrid.base.constructor.call(this, conf, parent); // Call the base constructor

            // Always create the body to hold the property rows
            this.createBody(conf.body);

            if (conf.metadata) {
                this.setMetadata(conf.metadata);
            }

            if (conf.record) {
                this.setRecord(conf.record);
            }

            // Always create the footer to display the field description
            this.createFooter(conf.footer);
        },

        type: type,

        setMetadata: function (metadata) {

            var me = this,
                body = me.body,
                group;

            //            if (util.isEquivalent(this.metadata, metadata)) { Enable when implemented
            //                return;
            //            }

            me.metadata = metadata;
            body.clear(); // Clear the children

            metadata.groups.forEach(function (groupConf) {
                group = Xjs.create('propertyGroup', groupConf, me);
                body.addChild(group);
            });
        },

        setRecord: function (record) {
        }
    });

    Xjs.registerType(PropertyGrid);

    return PropertyGrid;
});